package com.nami.logic.service.impl;

import com.nami.logic.cache.group.GroupCache;
import com.nami.logic.service.GroupService;
import com.nami.store.api.service.RemoteGroupService;
import com.nami.store.api.response.group.GroupDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class GroupServiceImpl implements GroupService {

    private final RemoteGroupService remoteGroupService;


    @Override
    public GroupDTO getGroupFromCache(String appKey, String groupId) {
        if (StringUtils.isBlank(appKey) || StringUtils.isBlank(groupId)) {
            return null;
        }
        GroupDTO groupVO = GroupCache.get(groupId);
        if (groupVO == null) {
            GroupDTO exist = remoteGroupService.getGrpById(appKey, groupId);
            if (exist == null) {
                return null;
            }
            GroupCache.add(groupId, exist);
            return exist.clone();
        }
        return groupVO;

    }

}
